package demo;

import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    private Queue<Integer> qu1;
    private Queue<Integer> qu2;

    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    public void push(int x) {
        //入栈的时候入到不为空的队列里
        if (empty()) {
            qu1.offer(x);
        } else {
            if (!qu1.isEmpty()) {
                qu1.offer(x);
            }
            if (!qu2.isEmpty()) {
                qu2.offer(x);
            }
        }

    }

    public int pop() {
        if (empty()) {
            return -1;//栈为空
        }
        if (!qu1.isEmpty()) {
            int cnt = qu1.size() - 1;
            while (cnt != 0) {
                int tmp = qu1.poll();
                qu2.offer(tmp);
                cnt--;
            }
            return qu1.poll();
        }
        int cnt = qu2.size() - 1;
        while (cnt != 0) {
            int tmp = qu2.poll();
            qu1.offer(tmp);
            cnt--;
        }
        return qu2.poll();
    }

    public int top() {
        if (empty()) {
            return -1;
        }
        int tmp = 0;
        if (!qu1.isEmpty()) {
            int cnt = qu1.size();
            while (cnt != 0) {
                tmp = qu1.poll();
                qu2.offer(tmp);
                cnt--;
            }
            return tmp;
        }
        int cnt = qu2.size();
        while (cnt != 0) {
            tmp = qu2.poll();
            qu1.offer(tmp);
            cnt--;
        }
        return tmp;
    }

    public boolean empty() {
        //两个队列都为空表示栈为空
        if (qu1.isEmpty() && qu2.isEmpty()) {
            return true;
        }
        return false;
    }
}